package com.study.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Inbound;
import com.study.model.pojo.Intype;
import com.study.model.pojo.Product;
import com.study.model.pojo.Warehouse;
import com.study.services.InboundService;
import com.study.services.IntypeService;
import com.study.services.ProductService;
import com.study.services.WarehouseService;

@RestController
@CrossOrigin
public class InboundController {
	@Autowired
	InboundService iService;
	@Autowired
	IntypeService tService;
	@Autowired
	ProductService pService;
	@Autowired
	WarehouseService wService;

	/**
	 * 查询入库全部入库信息
	 * 
	 * @return
	 */
	@RequestMapping("/findinbound")
	public JSONObject findinbound(String intName) {
		JSONObject json = new JSONObject();
		JSONArray json1 = new JSONArray();
		JSONArray json2 = new JSONArray();
		JSONArray json3 = new JSONArray();
		JSONArray json4 = new JSONArray();
		System.out.println("111111");
		// 查询所有的入库信息
		if (intName==null||intName.length()==0||intName.equals("查询全部")) {
			List<Inbound> list = iService.findAll();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for (Inbound i : list) {
				JSONObject jo = new JSONObject();
				jo.put("inbId", i.getInbId());
				jo.put("proId", i.getProduct().getProName());
				jo.put("orderId", i.getOrderId());
				jo.put("warName", i.getWarName());
				jo.put("intName", i.getIntName());
				jo.put("inbDate", sdf.format(i.getInbDate()));
				jo.put("inbUnit", i.getInbUnit());
				jo.put("inbNum", i.getInbNum());
				jo.put("inbNote", i.getInbNote());
				jo.put("inbStatic", i.getInbStatic());
				jo.put("inbSupplier", i.getInbSupplier());
				json1.add(jo);
			}
		}else {
			List<Inbound> list=iService.findByintName(intName);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for (Inbound i : list) {
				JSONObject jo = new JSONObject();
				jo.put("inbId", i.getInbId());
				jo.put("proId", i.getProduct().getProName());
				jo.put("orderId", i.getOrderId());
				jo.put("warName", i.getWarName());
				jo.put("intName", i.getIntName());
				jo.put("inbDate", sdf.format(i.getInbDate()));
				jo.put("inbUnit", i.getInbUnit());
				jo.put("inbNum", i.getInbNum());
				jo.put("inbNote", i.getInbNote());
				jo.put("inbStatic", i.getInbStatic());
				jo.put("inbSupplier", i.getInbSupplier());
				json1.add(jo);
			}
		}
		// 查询所有的入库类型
		JSONObject jo1=new JSONObject();
		jo1.put("intId", "int0001");
		jo1.put("intName", "查询全部");
		json2.add(jo1);
		List<Intype> list2 = tService.findByintType("入库");
		for (Intype intype : list2) {
			JSONObject jo = new JSONObject();
			System.out.println(intype.getIntName());
			jo.put("intId", intype.getIntId());
			jo.put("intName", intype.getIntName());
			jo.put("intType", intype.getIntType());
			json2.add(jo);
		}
		// 仓库名称
		List<Warehouse> list3 = wService.findAll();
		for (Warehouse w : list3) {
			JSONObject jo = new JSONObject();
			jo.put("warId", w.getWarId());
			jo.put("warName", w.getWarName());
			jo.put("warKind", w.getWarKind());
			json3.add(jo);
		}
		//查询产品名称
		List<Product> list4=pService.findAll();
		for (Product p : list4) {
			JSONObject jo = new JSONObject();
			jo.put("proId", p.getProId());
			jo.put("proName", p.getProName());
			json4.add(jo);
		}
		json.put("inbound", json1);
		json.put("intype", json2);
		json.put("warehouse", json3);
		json.put("product", json4);
		return json;
	}

	/**
	 * 新增入库
	 * 
	 * @param inbound
	 * @param proId
	 * @return
	 */
	@RequestMapping("/addinbound")
	public JSONObject addinbound(Inbound inbound, String proId) {
		JSONObject json = new JSONObject();
		inbound.setInbId("inb0004");
		inbound.setInbStatic("待审核");
		// 通过前台参数查询产品id
		String proid = pService.findByproName(proId);
		// 装入map集合中
		HashMap map = new HashMap<>();
		map.put("inbId", inbound.getInbId());
		map.put("proId", proid);
		map.put("orderId", inbound.getOrderId());
		map.put("warName", inbound.getWarName());
		map.put("intName", inbound.getIntName());
		map.put("inbUnit", inbound.getInbUnit());
		map.put("inbNum", inbound.getInbNum());
		map.put("inbNote", inbound.getInbNote());
		map.put("inbStatic", inbound.getInbStatic());
		map.put("inbSupplier", inbound.getInbSupplier());
		System.out.println(map);
		String returnString=iService.inbound(map, proid, inbound);// 新增入库
		json.put("ans", returnString);
		return json;
	}

	/**
	 * 根据id删除入库信息
	 * 
	 * @param inbId 入库编号
	 * @return
	 */
	@RequestMapping("/delinbound")
	public JSONObject delinbound(String inbId) {
		JSONObject json = new JSONObject();
		System.out.println(inbId);
		iService.delInbound(inbId);
		json.put("ans", "y");
		return json;
	}

	/**
	 * 修改入库信息
	 * 
	 * @param inbound
	 * @return
	 */
	@RequestMapping("/updateinbound")
	public JSONObject updateinbound(Inbound inbound, String proId) {
		JSONObject json = new JSONObject();
		System.out.println("proID======" + proId);
		// 通过前台参数查询产品id
		String proid = pService.findByproName(proId);
		// 装入map集合中
		HashMap map = new HashMap<>();
		map.put("inbId", inbound.getInbId());
		System.out.println("idididididiid"+inbound.getInbId());
		map.put("proId", proid);
		map.put("orderId", inbound.getOrderId());
		map.put("warName", inbound.getWarName());
		map.put("intName", inbound.getIntName());
		map.put("inbUnit", inbound.getInbUnit());
		map.put("inbNum", inbound.getInbNum());
		map.put("inbNote", inbound.getInbNote());
		map.put("inbSupplier", inbound.getInbSupplier());
		iService.editInbound(map);//修改信息
		json.put("ans", "y");
		return json;
	}
	
	/**
	 * 通过审核
	 * @param inbId
	 * @return
	 */
	@RequestMapping("/pass")
	public JSONObject pass(String inbId) {
		JSONObject json = new JSONObject();
		iService.pass(inbId);
		json.put("ans", "y");
		return json;
	}
	
}
